<template xmlns="http://www.w3.org/1999/html">
  <div class="content">
    <div class="title">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>我的消息</b></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="main-content">
      <el-col style="margin-bottom: 20px">
        <el-date-picker format="yyyy-MM-dd" type="daterange" placeholder="选择日期" v-model="messageDate" style="margin-right: 20px"></el-date-picker>
        <el-button type="primary" @click="queryMessage" icon="search">搜索</el-button>
      </el-col>
      <el-col style="margin-bottom: 20px">
        <el-button type="primary" @click="markHaveRead" v-if="count>=2">标为已读</el-button>
      </el-col>
      <el-table :data="messageList" highlight-current-row @select="selectCount" @select-all="selectAll" @sort-change="change">
        <el-table-column type="selection">
        </el-table-column>
        <el-table-column prop="gmtCreate" label="消息日期" width="220" sortable="custom">
        </el-table-column>
        <el-table-column prop="messageContent" label="消息内容" width="200">
        </el-table-column>
        <el-table-column prop="options" label="操作" min-width="180">
          <template scope="scope">
            <el-button v-if="scope.row.status=='0'" @click="markRead(scope.$index,scope.row)" type="text" size="small">标为已读</el-button>
            <span v-else style="color: gray">已读</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination 
         layout="sizes,prev, pager, next" @current-change="getCurrentPage"
         :page-sizes="[10,20,30,50]" :total="total"
         :page-size="pagesize"
         :current-page.sync="currentPage"
         @size-change="sizeChange"
         style="text-align: center;margin-top: 20px">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import ElCol from 'element-ui/packages/col/src/col'
  import axios from 'axios'
  import $ from 'jquery'

  export default {
    components: {
      ElCol},
    data () {
      return {
        messageDate:'',
        pagesize: 10,
        currentPage:1,
        total: 10,
        count:0,
        selectedRow:[],
        messageList:[],
        sort:''
      }
    },
    methods:{
      formatDate(date, fmt) {
          if (/(y+)/.test(fmt)) {
              fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
          }
          let o = {
              'M+': date.getMonth() + 1,
              'd+': date.getDate(),
              'h+': date.getHours(),
              'm+': date.getMinutes(),
              's+': date.getSeconds()
          };
          for (let k in o) {
              if (new RegExp(`(${k})`).test(fmt)) {
                  let str = o[k] + '';
                  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
              }
          }
          return fmt;
      },

      padLeftZero(str) {
          return ('00' + str).substr(str.length);
      },
      //获取消息列表
      getMessageList(){
        this.$http.get("/messages/list",{
          params:{
            pageNo:this.currentPage,
            pageSize:this.pagesize
          }
        })
        .then((respnse)=>{
          this.messageList = respnse.data.data
          this.total=respnse.data.totalCount
          console.log(respnse)
        })
        .catch(function(error){
          console.log(error)
        })
      },
      //单个标记已读
      markRead(index, row){
        console.log(row)
        let that = this
        this.$http.post("/messages/status/"+row.id)
        .then((respnse)=>{
          console.log(respnse)
          if(respnse.data.success){
            this.$message('操作成功')
            that.getMessageList()
          }else{
            this.$message('操作失败')
          }
        })
        .catch(function(error){
          console.log(error)
        })
      },
      //选择单个checkbox
      selectCount(row){
        this.count = row.length
        this.selectedRow = row
        console.log(row)
        console.log(this.count)
      },
     //点击批量标记已读按钮
      markHaveRead(){
        console.log(this.selectedRow)
        var mesId = []
        $.each(this.selectedRow , function(index , value){
          mesId.push(value.id)
        })
        this.$http.post("/messages/status",mesId)
        .then((respnse)=>{
          console.log(respnse)
        })
        .catch(function(error){
          console.log(error)
        })
      },
      //查询
      queryMessage(){
        this.$http.get("/messages/list",{
          params:{
            gmtStart:this.formatDate(this.messageDate[0],'yyyy-MM-dd'),
            gmtEnd:this.formatDate(this.messageDate[1],'yyyy-MM-dd'),
            sort:this.sort,
            pageNo:this.currentPage,
            pageSize:this.pagesize
          }
        })
        .then((respnse)=>{
          this.messageList = respnse.data.data
          this.total=respnse.data.totalCount
        })
        .catch(function(error){
          console.log(error)
        })
      },
      //点击全选
      selectAll(selection){
        console.log(selection)
        this.selectedRow = selection
        this.count = selection.length
        console.log("all:"+this.count)
      },

      change(){
        this.sort = 'desc'
         this.$http.get("/messages/list",{
          params:{
            sort:this.sort,
            pageNo:this.currentPage,
            pageSize:this.pagesize
          }
        })
        .then((respnse)=>{
          this.messageList = respnse.data.data
          this.total=respnse.data.totalCount
        })
        .catch(function(error){
          console.log(error)
        })
      },

      getCurrentPage(currentPage){
        this.currentPage = currentPage
        this.getMessageList()
      },

      sizeChange(val){
        this.pagesize = val
        this.getMessageList()
      }
    },

    mounted() {
      this.getMessageList()
    },
    beforeMount(){
     
    }
  }
</script>

<style lang="scss" scoped>
.content {
  padding: 10px;
  background: #F7F7F7;
  .title {
    color: #99A9BF;
    font-size: 14px;
    line-height: 18px;
    margin: 8px 0px 10px 0px;
  }
  .main-content {
    background: #fff;
    position: fixed;
    top: 96px;
    bottom: 10px;
    left: 216px;
    right: 16px;
    overflow: auto;
    padding: 20px;
  }
}
</style>
